/**
 * @file      https://blog.csdn.net/shaderdx/article/details/53693978
 * @copyright Copyright (c) 2017, Unitech Co., Ltd. All Rights Reserved.
 * @brief     brief
 * @author    chenjiaqi
 * @date      2017-06-20 11:55:51
 *
 * @note
 *     defines
 */

#pragma once
#include "header.h"

class CThread
{
public:
    enum THREADSTATE
    {
        STATE_RUN,
        STATE_BLOCK,
        STATE_SUSPEND,
        STATE_READY,
        STATE_DEAD
    };

private: 
    int                m_errorcode;
    int                m_threadID;
    char              *m_name;
    bool               m_detach;
    bool               m_createSuspend;

    THREADSTATE        m_state;

    semaphore_t        m_handSem;

public:
    CThread();
    ~CThread();
    CThread(bool detach, bool suspend);


public:
    virtual void       run(void) = 0;

    void               setThreadState(THREADSTATE state){ m_state = state; }
    THREADSTATE        getThreadState(){ return m_state; }
    int                getLastError(){ return m_errorcode; }
    int                setThreadName(char* name){ strcpy_s(m_name, 100, name); }
    char*              getThreadName(){ return m_name; }
    int                getThreadID(){ return m_threadID; }


    bool               terminate(void);
    bool               start(void);
    bool               exit(void);
    bool               wakeup(void);

    bool               setPriorty();
    int                getPriorty();
    int                getConcurency();
    bool               setConcurency(int num);

    bool               detach();
    bool               join();
    bool               yield();
    int                self();


protected:
    void               setErrorCode(int code){ m_errorcode = code; };
    void*              threadFunction(void *);




};


